#include "gamecontroller.h"
#include "gameconfig.h"

#include <QSound>

GameController *GameController::s_instance = nullptr;
GameController::GameController()
    :m_mainScene(nullptr)
    ,m_gameScene(nullptr)
    ,m_leveScene(nullptr)
{




}

GameController::~GameController()
{
    //这里不写对指针的删除是因为这是单列，并且Qt比较智能帮助处理了
//    if(s_instance != nullptr)
//        delete s_instance;

}
//单例实现
GameController *GameController::getInstance()
{
    if(s_instance == nullptr)
    {
        s_instance = new GameController;
    }

    return s_instance;
}

//mianscene界面的初始化
void GameController::initMainScene(MainScene *scene)
{
    m_mainScene = scene;
}


//mainScene的展现与隐藏
void GameController::showMianScene()
{
    if(nullptr != m_mainScene)
    {
        m_mainScene->move(getx(),gety());
        m_mainScene->show();
    }
}

void GameController::hideMianScene()
{
    if(nullptr != m_mainScene)
    {
        setxy(m_mainScene->x(),m_mainScene->y());
        m_mainScene->hide();
    }
}


//levelscene的展现与隐藏
void GameController::showLevelScene()
{
    if(nullptr == m_leveScene)
    {
        m_leveScene = new Levelscene();
    }
    m_leveScene->move(getx(),gety());
    m_leveScene->show();
}

void GameController::hideLevelScene()
{
    if(nullptr != m_leveScene)
    {
        setxy(m_leveScene->x(),m_leveScene->y());
        m_leveScene->hide();
    }
}



//gamescene的展现与隐藏
void GameController::showGameScene(int level )
{
    if(nullptr == m_gameScene)
    {
        m_gameScene = new Gamescene(level);
    }
    m_gameScene->move(getx(),gety());
    m_gameScene->show();
}

void GameController::hideGameScene()
{
    if(nullptr != m_gameScene)
    {
        setxy(m_gameScene->x(),m_gameScene->y());
        m_gameScene->hide();
    }
}



//删除gamescene界面
void GameController::deletGameScene()
{
    if(m_gameScene != nullptr)
    {
        setxy(m_gameScene->x(),m_gameScene->y());
        delete m_gameScene;
    }
    m_gameScene = nullptr;
}

void GameController::setxy(double xx, double yy)
{
    x = xx;
    y = yy;
}

double GameController::getx()
{
    return x;
}

double GameController::gety()
{
    return y;
}


void GameController::playStarsSound()
{
    QSound::play(SOUND_TAP_BUTTON);
}

void GameController::playBackSound()
{
    QSound::play(SOUND_BACK_BUTTON);
}

void GameController::playCoinSound()
{
    QSound::play(SOUND_COIN_FLIP);
}

void GameController::playWinSound()
{
    QSound::play(SOUND_LEVEL_WN);
}
